#include "rclcpp/rclcpp.hpp"
#include "autopartol_interface/srv/speech_text.hpp"
#include <string>

using autopartol_interface::srv::SpeechText;
using namespace std::placeholders;

class Speaker : public rclcpp::Node
{
private:
    rclcpp::Service<SpeechText>::SharedPtr speaker_;
    void speech_text_callback(const SpeechText::Request::SharedPtr req, const SpeechText::Response::SharedPtr res)
    {
        RCLCPP_INFO(this->get_logger(), "语音播报内容：%s", req->text.c_str());
        std::string command = "espeak \"" + req->text + "\"";
        int ret = std::system(command.c_str()); //0->suc,1->fal
        res->result = (ret == 0);
        RCLCPP_INFO(this->get_logger(), "语音生成：%s", res->result ? "succeeded" : "failed");
    }

public:
    Speaker() : Node("speaker_node")
    {
        speaker_ = this->create_service<SpeechText>("speech_text", std::bind(&Speaker::speech_text_callback, this, _1, _2));
    }
};

int main(int argc, char *argv[])
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<Speaker>();
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}
